﻿#ifndef KINCOSERVOMANAGER_H
#define KINCOSERVOMANAGER_H

#include <QObject>
#include <QWebSocket>
class KincoServoManager : public QObject
{
    Q_OBJECT
public:
    explicit KincoServoManager(QWebSocket *cl,QObject *parent = nullptr);

    /************************常用字典索引(最后表示该参数的数据类型)*****************/
    const static uint32_t RO_WorkMode_int8 = 0x606100;//工作模式
    const static uint32_t RO_StateWord_u16 = 0x604100;    //状态字
    const static uint32_t RO_RealPosition_int32 = 0x606300;     //实际位置
    const static uint32_t RO_RealSpeed_int32 = 0x606C00;    //实际速度
    const static uint32_t RO_RealCurrent_int16 = 0x607800;  //实际电流
    const static uint32_t RO_WarnStateWord_u16 = 0x268000;    //警告状态字
    const static uint32_t RO_PositiveLimit_int32 = 0x20101B;    //正限位位置
    const static uint32_t RO_NagetiveLimit_int32 = 0x20101C;    //负限位位置

    const static uint32_t RW_WorkMode_int8 = 0x606000;     //工作模式
    const static uint32_t RW_ControlWord_u16 = 0x604000;  //控制字
    const static uint32_t RW_TargetPosition_int32 = 0x607A00;   //目标位置
    const static uint32_t RW_TrapeSpeed_u32 = 0x608100;   //梯形速度
    const static uint32_t RW_TrapeAcc_u32 = 0x608300; //梯形加速度
    const static uint32_t RW_TrapeDec_u32 = 0x608400; //梯形减速度
    const static uint32_t RW_TargetSpeed_int32 = 0x60FF00;  //目标速度
    const static uint32_t RW_TargetTorque_int16 = 0x607100; //目标扭矩
    const static uint32_t RW_EncoderReset_u8 = 0x269000; //编码器数据复位
    const static uint32_t RW_OriginMode_u8 = 0x609800; //原点模式
    const static uint32_t RW_CanRate_u8 = 0x2F8100; //can波特率
    const static uint32_t RW_CanHeartTime_u16 = 0x101700; //can心跳报文产生时间
    const static uint32_t RW_ServoBreak_u8 = 0x641017;  //电机附件，1-带抱闸，0-不带抱闸，默认0
    const static uint32_t RW_DeviceReboot_uint16 = 0x2FFF00;//驱动器重启
    const static uint32_t RW_Save_uint8 = 0x101001;//存储所有参数
    /***********************工作模式**********************************/
    const static int8_t WorkMode_Pulse = -4;//脉冲模式
    const static int8_t WorkMode_ImmediateSpeed = -3;//立即速度模式
    const static int8_t WorkMode_Location = 1;//位置模式
    const static int8_t WorkMode_Speed = 3;//速度模式
    const static int8_t WorkMode_Torque = 4;//力矩模式
    const static int8_t WorkMode_FindOrigin = 6;//找原点模式
    const static int8_t WorkMode_Runin = 7;//插补模式

    /***********************can波特率********************************/
    const static uint8_t CanRate_1M = 100; //1M
    const static uint8_t CanRate_500K = 50; //500k
    const static uint8_t CanRate_250K = 25; //250k
    const static uint8_t CanRate_125k = 12; //125k
    const static uint8_t CanRate_50k = 5; //50k

    /**********************状态字位定义*********************************/
    const static uint16_t Status_PowerReady = 0x0001;//bit0：准备上电
    const static uint16_t Status_PowerOn = 0x0002;//bit1：已上电
    const static uint16_t Status_Enable = 0x0004;//bit2：使能
    const static uint16_t Status_Fault = 0x0008;//bit3：故障
    const static uint16_t Status_FbdOutVoltage = 0x0010;//bit4：禁止输出电压
    const static uint16_t Status_QuickStop = 0x0020;//bit5：快速停止
    const static uint16_t Status_PowerForbid = 0x0040;//bit6：上电禁止
    const static uint16_t Status_Warning = 0x0080;//bit7：警告
    const static uint16_t Status_RemoteControl = 0x0200;//bit9：远程控制
    const static uint16_t Status_TargetPositionArrive = 0x0400;//bit10：目标位置到
    const static uint16_t Status_InterLimitEnable = 0x0800;//bit11：内部限位激活
    const static uint16_t Status_PulseResponse = 0x1000;//bit12：脉冲响应
    const static uint16_t Status_OriginError = 0x2000;//bit13：跟随误差/原点错误
    const static uint16_t Status_MotorExcitation = 0x4000;//bit14：找到电机励磁
    const static uint16_t Status_OriginFind = 0x8000;//bit15：原点找到

    /**************************控制字定义*********************************/
    const static uint8_t ControlWord_MotorPowerOff  = 0x06; //电机断电
    const static uint8_t ControlWord_MotorPowerOn  = 0x0F; //电机上电
    const static uint8_t ControlWord_ShutDown  = 0x0B; //快速停止，负载停止，电压断开
    const static uint8_t ControlWord_AbsoluteLocationStep1  = 0x2F; //进入绝对定位指令1
    const static uint8_t ControlWord_AbsoluteLocationStep2  = 0x3F; //进入绝对定位指令2
    const static uint8_t ControlWord_RelativeLocationStep1  = 0x4F; //进入相对定位指令1
    const static uint8_t ControlWord_RelativeLocationStep2  = 0x5F; //进入相对定位指令2
    const static uint16_t ControlWord_Follow  = 0x103F; //根据目标位置变化立即绝对定位
    const static uint8_t ControlWord_OringLocationStep1  = 0x0F; //进入原点定位指令1
    const static uint8_t ControlWord_OringLocationStep2  = 0x1F; //进入原点定位指令2
    const static uint8_t ControlWord_ClearInterFault  = 0x86; //清除内部故障

    /***************************其他参数*********************************/
    const static uint16_t Comman_Reboot = 0xAA55;    //驱动器重启
    const static uint8_t Command_Save = 0x01;   //保存参数
    constexpr static float AxisX_1mm_INC = 1968.5;  //X轴1mm所对应的脉冲
    constexpr static float Ratio_SpeedXY = 0.5;  //speedY/SpeedX
    //构建json设定参数数据
    QString GetSetParamsJson(int devicno,int32_t objctindex,int32_t value);
    //构建json控制指令数据
    QString GetSetControlJson(uint8_t deviceno,uint8_t value);
    //设定某个参数
    bool SetParams(int devicno,int32_t objctindex,int32_t value);
    //发送控制指令
    bool SetControl(uint8_t deviceno,uint8_t value);
    //请求上传只读、读写参数
    bool SetProbe(uint8_t deviceno,uint8_t type);
    //发送坐标移动指令
    bool SetMoveCmd(QString type,int value1,int value2,QString word);

    void setWebsocket(QWebSocket *&);
signals:

private:
    QWebSocket  *pKincoSocket {nullptr};
};

#endif // KINCOSERVOMANAGER_H
